package com.dq.domain.goods;

import com.alibaba.fastjson.JSONObject;
import com.dq.context.Constant;
import com.dq.vo.baseVo.VO;
import com.dq.domain.annex.MallAnnex;
import com.dq.domain.base.BaseEntity;

import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;
import java.math.BigDecimal;
import java.util.List;

/**
 * @创建人 gewenzhen
 * @创建时间 2019/9/16
 * @描述
 */
@Data
@Entity
@ApiModel(value = "产品")
public class Product extends BaseEntity implements VO.ToVO {
    @ApiModelProperty(value = "产品编号", hidden = true)
    protected String productNo = generyStrAndDatetimeAndRandomNum("Product_", Constant.ORDERS_SUFF_COUNT);

    @ApiModelProperty(value = "商品id", hidden = true)
    @ManyToOne
    protected Goods goods;

    @ApiModelProperty("是否默认")
    protected Boolean isDefault;

    @ApiModelProperty(value = "原价")
    protected BigDecimal marketPrice;
    @ApiModelProperty(value = "购买价格")
    protected BigDecimal price;
    @ApiModelProperty("序列码")
    protected String identifier;

    @ApiModelProperty(value = "商品规格ids,以,号分割")
    protected String goodSpecIds;
    @ApiModelProperty("SKU")
    protected String sku;


    @ApiModelProperty("仅用来展示门店商品库存")
    @Transient
    public Long goodsStock;
    @ApiModelProperty("仅用来展示商品库存中间表变更时间")
    @Transient
    public Long changeTime;


    @Data
    @ApiModel(value = "产品视图")
    public static class Vo implements VO {
        @ApiModelProperty(value = "产品id")
        private Long id;
        @ApiModelProperty(value = "原价")
        private BigDecimal marketPrice;
        @ApiModelProperty(value = "购买价格")
        private BigDecimal price;
        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "更新时间")
        private String updateTime;
        @ApiModelProperty("序列码")
        private String identifier;
        @ApiModelProperty("sku")
        private String sku;
        @ApiModelProperty("商品id")
        private Long goodId;
        @ApiModelProperty("商品名称")
        private String goodName;
        @ApiModelProperty("商品状态")
        protected String statusEn;
        @ApiModelProperty("商品状态")
        protected String statusCn;
        @ApiModelProperty("商品展示状态")
        protected String showStatus;
        @ApiModelProperty("商品展示状态")
        protected String showStatusEn;
        @ApiModelProperty("是否推荐")
        protected String isRecommend;
        @ApiModelProperty("商品描述")
        protected String goodsDescription;
        @ApiModelProperty("商品轮播图")
        protected List<MallAnnex.Vo> mallList;
        @ApiModelProperty("商品规格")
        private List<GoodSpecification.Vo> goodSpecificationListVo;

        @ApiModelProperty("分类名称")
        private String categoryName;

        @ApiModelProperty("门店商品库存")
        protected Long goodsStock;
        @ApiModelProperty("数字备用字段")
        protected Long resNum;
        @ApiModelProperty("商品库存中间表变更时间, pc端搜索条件")
        protected String changeTime;
        @ApiModelProperty("仅用来展示")
        public Long orgId;
        @ApiModelProperty("仅用来展示")
        public String orgName;
        @ApiModelProperty(value = "缩略图")
        private String smallImgUrl;
        @ApiModelProperty(value = "列表图")
        private String listImgUrl;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.marketPrice = fmt(this.marketPrice);
        vo.price = fmt(this.price);

        vo.identifier = this.identifier;
        vo.goodName = this.goods == null ? "" : this.goods.getGoodsName();
        vo.categoryName = this.goods.getCategory() == null ? "" : this.goods.getCategory().getTitle();
        vo.goodId = this.goods == null ? null : this.goods.getId();
        vo.sku = fmt(this.sku);
        vo.statusEn = fmt(this.getGoods().getGoodsStatus().toString());
        vo.statusCn = fmt(this.getGoods().getGoodsStatus().name);
        vo.showStatus = fmt(this.getGoods().getShowStatus().name);
        vo.showStatusEn = fmt(this.getGoods().getGoodsStatus().toString());
        vo.isRecommend = this.getGoods().isRecommend==null?"未推荐":this.getGoods().isRecommend==true?"推荐":"未推荐";
        vo.goodsDescription =this.getGoods().getGoodsDescription();
        vo.sku = this.sku;
        vo.smallImgUrl = this.getGoods().getSmallImgUrl();
        vo.listImgUrl = this.getGoods().getListImgUrl();
        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.updateTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        vo.goodsStock = fmt(this.getGoodsStock());

        // 备用展示
        vo.resNum = 1L;

        return vo;
    }
    public JSONObject toStockVO() {
        JSONObject vo = new JSONObject();
        vo.put("id", this.id);
        vo.put("marketPrice", fmt(this.marketPrice));
        vo.put("price", fmt(this.price));
        vo.put("sku", fmt(this.sku));

        vo.put("goodId", this.getGoods().getId());
        vo.put("goodName", this.getGoods().getGoodsName());

        // 关联  库存
        vo.put("goodsStock", fmt(this.getGoodsStock()));
        vo.put("changeTime", TimeUtil.longToString(this.changeTime, TimeUtil.YYYY_MM_DD_HH_MM_SS));
        vo.put("orgId", this.getGoods().getOrgId());
        vo.put("orgName", this.getGoods().getOrgName());

        return vo;
    }
    public JSONObject toJson() {
        JSONObject vo = new JSONObject();
        vo.put("id", this.getId());
        vo.put("goodName", this.goods == null ? "" : this.goods.getGoodsName());
        vo.put("goodId", this.goods == null ? null : this.goods.getId());
        vo.put("sku", fmt(this.getSku()));
        return vo;
    }

}
